<?php

namespace App\Controller\App;

use App\Base\BaseCache;
use App\Base\BaseController4App;
use App\Base\Format;
use App\Entity\PayLog;
use App\Entity\User;
use App\Model\LazyCommon\MiniAppModel;
use App\Model\LazyCommon\RulesModel;
use App\Model\LazyCommon\UserModel;
use App\Model\LazyCommon\WechatPayModel;
use App\Model\LiveModel;
use App\Model\PayLogModel;
use App\Model\TopicModel;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\HttpFoundation\Request;

/**
 * @route("/app")
 */
class OrderController extends BaseController4App
{
    /**
     * @route("/order/create",name="AppOrderCreate")
     */
    public function AppOrderCreate()
    {
        $userId = $this->needToBeUser();
        if (!$id = $this->getParamRequired("id")) {return $this->json_out(0,"参数缺失");}
        if (!$type = $this->getParamRequired("type")) {$type = 1;}

        $PayLogModel = new PayLogModel($this->getDoctrine());
        $LiveModel = new LiveModel($this->getDoctrine());
        switch ($type){
            case 1://直播课程付费
                //检查是否购买过
                $log = $PayLogModel->getByKVs(["userId"=>$userId,"flag"=>1,"outId"=>$id,"status"=>200]);
                if ($log) {
                    return $this->json_out(1,"您已经购买过","1");
                }
                $live = $LiveModel->getById($id);
                $payLog = $PayLogModel->createOrder($userId,$live->getPrice(),$this->toArray($live)
                    ,101,1, $id);
                $sn = $payLog->getSn();
                $money = (int)($payLog->getMoney()*100);
                $WechatPayModel = new WechatPayModel($this->getDoctrine());
                $re = $WechatPayModel->createOrder("","直播课报名",$sn,$money);

                $this->data["re"] = $re;
                $this->data["info"] = unserialize($payLog->getOrderInfo());
                $this->data["sn"] = $sn;

                return $this->json_out(1,"OK",$this->data);
            case 2:
                $RulesModel = new RulesModel($this->getDoctrine());
                $rule = $RulesModel->getByK("CHARGE_CONFIG");
                $conf = unserialize($rule->getV());
                $chargeConf = $conf[$id - 1];//id传数组的index

                $payLog = $PayLogModel->createOrder($userId,$chargeConf["price"],$chargeConf
                    ,101,2,$id - 1);
                $sn = $payLog->getSn();
                $money = (int)($payLog->getMoney()*100);
                $WechatPayModel = new WechatPayModel($this->getDoctrine());
                $re = $WechatPayModel->createOrder("","会员充值",$sn,$money);

                $this->data["re"] = $re;
                $this->data["info"] = unserialize($payLog->getOrderInfo());
                $this->data["sn"] = $sn;
                return $this->json_out(1,"OK",$this->data);

        }
    }

    /**
     * @route("/check/payed",name="AppCheckIsPayed")
     */
    public function AppCheckIsPayed()
    {
        $userId = $this->needToBeUser();

        if (!$id = $this->getParamRequired("id")) {return $this->json_out(0,"参数缺失");}

        $PayLogModel = new PayLogModel($this->getDoctrine());
        $log = $PayLogModel->getByKVs(["userId"=>$userId,"flag"=>1,"outId"=>$id,"status"=>200]);
        if ($log) {
            return $this->json_out(1,"购买过","1");
        }else{
            return $this->json_out(1,"没购买过","0");
        }
    }





}
